package module.scene.view.iso
{
	import code.consts.DirectionConst;
	import code.utils.DirectionUtil;
	
	import com.darwin.darwinDisplay.DarBitmapMovieNode;
	import com.darwin.darwinDisplay.DarBitmapMovieNodeEvent;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.DllItem;
	import com.darwin.darwinDll.core.ResKey;
	import com.darwin.darwinIsometric.core.IsoObject;
	
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	
	import module.scene.consts.SceneConst;
	import module.scene.model.dataObj.IsoDataObj;
	import module.scene.model.dataObj.TroopIsoDataObj;
	import module.scene.view.iso.base.JusticeIsoObject;

	public class TroopIsoObject extends JusticeIsoObject
	{
		public var troopId:String = "";
		public var speed:Number = 2;
		private var _heroId:String;
		private var _armsId:String;
		private var _heroNode:DarBitmapMovieNode;
		private var _armsNode:DarBitmapMovieNode;
		private var _dic:Dictionary;
		public function TroopIsoObject(isoDataObj:TroopIsoDataObj,dataObj:Object = null)
		{
			super(isoDataObj,dataObj);
			_dic = new Dictionary();
			_heroId = isoDataObj.heroId;
			_armsId = isoDataObj.armsId;
			direction = isoDataObj.direction;
			troopId = isoDataObj.troopId;
			fieldVisibleRange = floorWidth*2;
			openFieldVisible = true;
			clickEnabled = true;
			mouseHoverEnabled = true;
			hideWithFog = true;
			init();
		}
		/**
		 * 只支持0 - 359.99999999999999。。。。。。。。。。的输入
		 * @param direction
		 * @return 
		 * 
		 */		
		override public function set direction(value:int):void
		{
			super.direction = value;
			if(_armsNode != null && _heroNode != null)
			{
				init();
			}
		}
		override public function dispose():void
		{
			super.dispose();
			_dic = null;
			if(_armsNode)
			{
				_armsNode.destroy();
			}
			if(_heroNode)
			{
				_heroNode.destroy();
			}
		}

	
		/**
		 *根据不同的信息得到不同的方位 
		 * 
		 */		
		private function init():void
		{
			removeDarNode(_heroNode);
			removeDarNode(_armsNode);
			_heroNode = null;
			_armsNode = null;
			checkAndLoadMats(_heroId);
			checkAndLoadMats(_armsId);
		}
		
		private function checkAndLoadMats(serverId:String):void
		{
			var $directionStr:String = DirectionUtil.directionToDirectionStr(direction);
			if(_dic[$directionStr + "_" + serverId] != null)
			{
				if(serverId == _heroId)
				{
					_heroNode = _dic[$directionStr + "_" + serverId] as DarBitmapMovieNode;
					addDarNode(_heroNode);
				}else{
					_armsNode = _dic[$directionStr + "_" + serverId] as DarBitmapMovieNode;
					addDarNode(_armsNode);
				}
			}else{
				var $isMirrorDirection:Boolean = DirectionUtil.isMirrorDirectionStr($directionStr);
				if($isMirrorDirection== true)
				{
					$directionStr =  DirectionUtil.mirrorToOriginal($directionStr);
				}
				var $other:Object = {"target":serverId,"direction":$directionStr,"isMirror":$isMirrorDirection};
				Dll.getInstance().getMatRes(serverId.toString(),onMatComplete,"MATERIAL_" + serverId+"_"+$directionStr+"_MOVE",0,DllItem.TYPE_SWF,$other);
			}
		}

		private function onMatComplete(data:Object):void
		{
			if(disposed==true)return;//物件已被销毁,不做后续处理
			
			var $resKey:ResKey = data.resKey as ResKey;
			var $directionStr:String = $resKey.other.direction;
			var $isMirror:Boolean =  $resKey.other.isMirror;
			var $targetId:String = $resKey.other.target;
			var $class:Class = data.data as Class;
			var $node:DarBitmapMovieNode = new DarBitmapMovieNode();
			$node = new DarBitmapMovieNode($class);
			$node.addEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,darwOnComplete);
			var $direct:String;
			if($isMirror)
			{
				$direct = DirectionUtil.originalToMirror($directionStr);
			}else{
				$direct = $directionStr;
			}
			if($targetId == _heroId)
			{
				_heroNode = $node;
				_dic[$direct + "_" + _heroId] = $node;
			}else if($targetId == _armsId)
			{
				_armsNode = $node;
				_dic[$direct + "_" + _armsId] = $node;
			}
			function darwOnComplete(e:DarBitmapMovieNodeEvent):void
			{
				$node.removeEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,darwOnComplete);
				if($isMirror)
				{
					$node = $node.mirror;
				}
				addDarNode($node);
			}
		}
		
		private function addDarNode(node:DarBitmapMovieNode):void
		{
			if(node && node.isComplete)
			{
				node.x = node.offsetPoint.x;
				node.y = node.offsetPoint.y;
				addNode(node);
				checkSwitchIndex();
			}
		}
		
		/**
		 *	检查并调整英雄和兵的层级 
		 */
		private function checkSwitchIndex():void
		{
			if(_armsNode && _armsNode.parentContainer && _heroNode && _heroNode.parentContainer)
			{
				if(getNodeAt(0) === _heroNode)
				{
					removeNode(_heroNode);
					addNode(_heroNode);
				}
			}
		}
		
		private function removeDarNode(node:DarBitmapMovieNode):void
		{
			if(node != null && node.parentContainer != null)
			{
				this.removeNode(node);
			}
		}

		override public function get isoDataObj():IsoDataObj
		{
			return _isoDataObj;
		}

	}
}